package ext.tianma.part.processors;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

import wt.csm.navigation.ClassificationNode;
import wt.method.RemoteAccess;
import wt.part.WTPart;
import wt.util.WTException;
import wt.util.WTProperties;

import com.infoengine.SAK.Task;
import com.infoengine.object.IeObject;
import com.infoengine.object.factory.Group;
import com.infoengine.util.IEException;
import com.ptc.core.components.beans.ObjectBean;
import com.ptc.core.components.forms.DefaultObjectFormProcessor;
import com.ptc.core.components.forms.FormProcessingStatus;
import com.ptc.core.components.forms.FormResult;
import com.ptc.core.lwc.server.LWCNormalizedObject;
import com.ptc.core.meta.common.TypeInstanceIdentifier;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.beans.NmCommandBean;

import ext.com.iba.IBAUtil;
import ext.generic.erp.attributes.AttributeUtil;
import ext.tianma.part.util.ClassificationUtil;

public class TwoRawTransRawProcessor extends DefaultObjectFormProcessor
		implements RemoteAccess, Serializable {
	private static boolean VERBOSE = false;
	private static String CLASSNAME = TwoRawTransRawProcessor.class.getName();
	private static final String RESOURCE = "ext.tianma.resource.ResourceRB";
	// 物料生效地
	private static final String PART_NUMBER_EFFECTIVE_SITE = "partNumberEffectiveSite";
	private static final String VIEW_A20 = "A20";
	private static String materialExcelPath = "";
	public static WTProperties wtProperties = null;
	static {
		try {
			WTProperties wtproperties = WTProperties.getLocalProperties();
			VERBOSE = wtproperties
					.getProperty("ext.tianma.part.verbose", false);

			wtProperties = WTProperties.getLocalProperties();
			materialExcelPath = wtProperties
					.getProperty("ext.tianma.part.materials");
		} catch (Throwable throwable) {
			throwable.printStackTrace(System.err);
			throw new ExceptionInInitializerError(throwable);
		}
	}

	@Override
	public FormResult doOperation(NmCommandBean arg0, List<ObjectBean> arg1)
			throws WTException {
		FormResult formresult = new FormResult();
		formresult.setStatus(FormProcessingStatus.SUCCESS);
		NmOid nmoid = arg0.getActionOid();
		WTPart currentPart = (WTPart) nmoid.getRefObject();
		if (currentPart != null) {
			System.out.println("currentNumber:" + currentPart.getNumber());
			Task task = new Task("/ext/generic/erp/query.xml");
			System.out.println("task:" + task);
			task.addParam("WHERE", "number='" + currentPart.getNumber() + "'");
			System.out.println("task:" + task);
			try {
				task.invoke();
				IeObject ieoRecord = null;
				Group group = task.getGroup("part");
				PrintWriter pw = new PrintWriter(System.out);
				group.toXML(pw, true, true);
				for (Enumeration enum1 = group.getGroup().getChildren(); enum1
						.hasMoreElements();) {
					ieoRecord = (IeObject) enum1.nextElement();

					String name = ieoRecord.getAtt("name").getDatum()
							.toString();
					String number = ieoRecord.getAtt("number").getDatum()
							.toString();
					String classification = ieoRecord.getAtt("classification")
							.getDatum().toString();
					System.out.println("Debug  二级料转换流程的物料编号：" + name
							+ ",number:" + number + ",classification："
							+ classification);
					
					String[] split = classification.split("\\\\");
					String firstClasses = split[1].replace(" ", "");
					String secondClasses = split[split.length-1].replace(" ", "");
					System.out.println("firstClasses:" + firstClasses + ",secondClasses" + secondClasses);
				}
			} catch (IEException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			String attr_name = "classification.id";
			LWCNormalizedObject obj = new LWCNormalizedObject(currentPart,
					null, null, null);
			obj.load(attr_name);
			TypeInstanceIdentifier classif_ref = (TypeInstanceIdentifier) obj
					.get(attr_name);
			System.out.println("Debug  二级料转换流程的物料编号：" + currentPart.getNumber()
					+ ",classif_ref:" + classif_ref);
			obj = new LWCNormalizedObject(classif_ref, null, null, null);
			obj.load("name", "description");
			//可以获取当前分类节点
			String name = (String) obj.get("name");
			String description = (String) obj.get("description");

		}
		return formresult;
	}
}
